package com.crscd.ai.controller;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.ToolResponseMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * Created with IntelliJ IDEA.
 *
 * @description:
 * @author: liuziyang
 * @since: 9/30/25 10:17 AM
 * @modifiedBy:
 * @version: 1.0
 */
@RestController
public class PromptController {
  @Resource(name = "deepseek")
  private ChatModel deepseekChatModel;

  @Resource(name = "qwen")
  private ChatModel qwenChatModel;

  @Resource(name = "deepseekChatClient")
  private ChatClient deepseekChatClient;

  @Resource(name = "qwenChatClient")
  private ChatClient qwenChatClient;

  @GetMapping("/prompt/chat")
  public Flux<String> chat(String question) {
    return deepseekChatClient
        .prompt()
        .system("你是一个法律助手，只回答法律问题，其他问题回复：我只能回答法律问题，其他无可奉告")
        .user(question)
        .stream()
        .content();
  }

  @GetMapping("/prompt/chat2")
  public Flux<ChatResponse> chat2(String question) {
    // 系统消息
    SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手，故事控制在300字以内");
    // 用户消息
    UserMessage userMessage = new UserMessage(question);
    Prompt prompt = new Prompt(userMessage, systemMessage);
    return deepseekChatModel.stream(prompt);
  }

  @GetMapping("/prompt/chat3")
  public Flux<String> chat3(String question) {
    // 系统消息
    SystemMessage systemMessage = new SystemMessage("你是一个讲故事的助手，故事控制在600字以内，以html的格式返回");
    // 用户消息
    UserMessage userMessage = new UserMessage(question);
    Prompt prompt = new Prompt(userMessage, systemMessage);
    return deepseekChatModel.stream(prompt)
        .mapNotNull(response -> response.getResults().getFirst().getOutput().getText());
  }

  @GetMapping("/prompt/chat4")
  public String chat4(String question) {
    AssistantMessage assistantMessage =
        Objects.requireNonNull(deepseekChatClient.prompt().user(question).call().chatResponse())
            .getResult()
            .getOutput();
    return assistantMessage.getText();
  }

  @GetMapping("/prompt/chat5")
  public String chat5(String city) {
    String answer =
        Objects.requireNonNull(
                deepseekChatClient.prompt().user(city + "未来三天天气情况如何?").call().chatResponse())
            .getResult()
            .getOutput()
            .getText();

    ToolResponseMessage toolResponseMessage =
        new ToolResponseMessage(List.of(new ToolResponseMessage.ToolResponse("1", "获得天气", city)));
    String toolResponse = toolResponseMessage.getText();
    return answer + toolResponse;
  }
}
